package com.bfsuol.yihang.documents.service;


import com.bfsuol.utils.FileUtils;
import com.bfsuol.yihang.documents.api.CommandDocumentService;
import com.bfsuol.yihang.documentFiles.domain.DocumentFilesService;
import com.bfsuol.yihang.documents.domain.DocumentService;
import com.bfsuol.yihang.documents.dto.DocumentDTO;
import com.bfsuol.yihang.documents.entity.DocumentEntity;
import com.bfsuol.yihang.documentFiles.entity.DocumentFilesEntity;
import com.bfsuol.yihang.file.domain.FileManagerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

@Service
public class CommandDocumentServiceImpl implements CommandDocumentService {

    @Autowired
    private DocumentService documentService;

    @Autowired
    private FileManagerService fileManagerService;
    @Autowired
    private DocumentFilesService documentFilesService;

    @Override
    public void save(DocumentDTO documentDTO) {
        DocumentEntity document = new DocumentEntity();
        BeanUtils.copyProperties(documentDTO,document);
        documentService.save(document);
    }

    @Override
    public void updateById(DocumentDTO documentDTO) {
        DocumentEntity document = new DocumentEntity();
        BeanUtils.copyProperties(documentDTO,document);
        documentService.updateById(document);
    }

    @Override
    public void deleteById(Long id) {
        documentService.removeById(id);
    }

    @Override
    public void saveOrUpdate(DocumentDTO documentDTO) {
        if(documentDTO.isNew()){
            this.save(documentDTO);
        }else{
            this.updateById(documentDTO);
        }
    }

    @Override
    public void uploadFiles(DocumentDTO documentDTO) {
        DocumentEntity documentEntity = documentService.getById(documentDTO.getId());
        String drivingBook = documentDTO.getDrivingBook();
        String regixCertificate = documentDTO.getRegixCertificate();
        String insurePicture = documentDTO.getInsurePicture();
        String repaymentPlan = documentDTO.getRepaymentPlan();
        if(StringUtils.isNotBlank(drivingBook)){
            Arrays.stream(drivingBook.split(",")).forEach(fileId->{
                uploadFiles(documentEntity.getVehicleNo(),fileId,"DRIVING_BOOK",documentDTO);
            });
        }
        if(StringUtils.isNotBlank(regixCertificate)){
            Arrays.stream(regixCertificate.split(",")).forEach(fileId->{
                uploadFiles(documentEntity.getVehicleNo(),fileId,"REGIX_CERTIFICATE",documentDTO);
            });
        }
        if(StringUtils.isNotBlank(insurePicture)){
            Arrays.stream(insurePicture.split(",")).forEach(fileId->{
                uploadFiles(documentEntity.getVehicleNo(),fileId,"INSURE_PICTURE",documentDTO);
            });
        }
        if(StringUtils.isNotBlank(repaymentPlan)){
            Arrays.stream(repaymentPlan.split(",")).forEach(fileId->{
                uploadFiles(documentEntity.getVehicleNo(),fileId,"REPAYMENT_PLAN",documentDTO);
            });
        }

    }

    private void uploadFiles(String vehicleNo,String fileId,String documentType,DocumentDTO documentDTO){
        String fileName = fileManagerService.getById(fileId).getFileTitle();
        String fileUrl = fileManagerService.getById(fileId).getFilePath();
        DocumentFilesEntity documentFilesEntity = new DocumentFilesEntity();
        documentFilesEntity.setDocumentId(documentDTO.getId());
        documentFilesEntity.setDocumentType(documentType);
        try {
            documentFilesEntity.setFile(FileUtils.toByteArray(fileUrl));
        } catch (IOException e) {
            e.printStackTrace();
        }
        documentFilesEntity.setFileName(fileName);
        documentFilesEntity.setCreateTime(new Date());
        documentFilesEntity.setUpdateTime(new Date());
        documentFilesEntity.setCreateUserName(documentDTO.getCreateUserName());
        documentFilesEntity.setUpdateUserName(documentDTO.getUpdateUserName());
        documentFilesEntity.setVehicleNo(vehicleNo);
        documentFilesService.save(documentFilesEntity);
    }
}
